package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.dao.BankcardTypeDao;
import com.baichang.taoshunda.dao.CashDao;
import com.baichang.taoshunda.dao.UserBankDao;
import com.baichang.taoshunda.entity.*;
import com.baichang.taoshunda.service.ParamsService;
import com.baichang.taoshunda.service.UserBankService;
import com.baichang.taoshunda.service.UserService;
import com.baichang.taoshunda.utils.ConvertTypeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class UserBankServiceImpl implements UserBankService {

    @Autowired
    private UserBankDao userBankDao;

    @Autowired
    private BankcardTypeDao bankcardTypeDao;
    @Autowired
    private CashDao cashDao;
    @Autowired
    private ParamsService paramsService;
    @Autowired
    private UserService userService;

    /**
     * 根据商家编号获取商家的银行卡
     *
     * @param bussId 商家编号
     * @return
     */
    @Override
    public List<Map<String, Object>> findBusinessBankCard(Integer bussId) {
        return userBankDao.findBusinessBankCard(bussId);
    }

    /**
     * 修改商家银行卡
     *
     * @param bankId     银行卡编号
     * @param bankTypeId 名称
     * @param bankNo     银行卡号
     * @return
     */
    @Override
    public boolean updateBusinessBankCard(Integer bankId, Integer bankTypeId, String bankNo) {
        return userBankDao.updateBusinessBankCard(bankId, bankTypeId, bankNo) > 0;
    }

    /**
     * 解绑银行卡
     *
     * @param bussId 商家编号
     * @param bankId 银行卡编号
     * @return
     */
    @Override
    public boolean unBundling(Integer bussId, Integer bankId) throws ServiceException {
        UserBank userBank = userBankDao.getEntityById(bankId);
        if (userBank == null) {
            throw new ServiceException("银行卡不存在");
        }
        if (!Objects.equals(userBank.getBussineId(), bussId)) {
            throw new ServiceException("您要解绑的银行卡不是您自己的银行卡");
        }
        return userBankDao.unBundling(bankId) > 0;
    }

    /**
     * 根据编号获取银行卡信息
     *
     * @param bankcardId 商家编号
     * @return
     */
    @Override
    public Map<String, Object> getUserBankById(Integer bankcardId) {
        return userBankDao.getUserBankById(bankcardId);
    }

    @Override
    public UserBank getUserBankBeanById(Integer bussId) throws ServiceException {
        UserBank userBank = userBankDao.getEntityById(bussId);
        if (userBank == null) {
            throw new ServiceException("银行卡不存在");
        }
        return userBank;
    }

    /**
     * 解绑银行卡
     *
     * @param disId  配送员编号
     * @param bankId 银行卡编号
     * @return
     */
    @Override
    public boolean unBundlingByDisId(Integer disId, Integer bankId) throws ServiceException {
        UserBank userBank = userBankDao.getEntityById(bankId);
        if (userBank == null) {
            throw new ServiceException("银行卡不存在");
        }
        if (!Objects.equals(userBank.getDeliveryId(), disId)) {
            throw new ServiceException("您要解绑的银行卡不是您自己的银行卡");
        }
        return userBankDao.unBundling(bankId) > 0;
    }

    /**
     * 修改银行卡信息
     *
     * @param bankId     银行卡编号
     * @param bankTypeId 银行卡类型编号
     * @param bankNo     银行卡号
     * @return
     */
    @Override
    public boolean updateBankCard(Integer bankId, Integer bankTypeId, String bankNo) {
        return userBankDao.updateBusinessBankCard(bankId, bankTypeId, bankNo) > 0;
    }

    /**
     * 查询配送员的所有银行卡
     *
     * @param disId 配送员编号
     * @return
     */
    @Override
    public List<Map<String, Object>> findDeliveryBankCard(Integer disId) {
        return userBankDao.findDeliveryBankCard(disId);
    }

    /**
     * 查看银行卡
     *
     * @return
     */
    @Override
    public List<BankcardType> findBank() {
        return bankcardTypeDao.findBankType();
    }

    /**
     * 绑定银行卡
     *
     * @param bussId     商家编号
     * @param cardTypeId 银行编号
     * @param cardNumber 银行卡号
     * @param userName   用户名
     * @return
     */
    @Override
    public boolean bussinessBundling(Integer bussId, Integer cardTypeId, String cardNumber, String userName) throws ServiceException {
        UserBank userBank = userBankDao.getuserBankByCardNumber(cardNumber);
        if (userBank != null) {
            throw new ServiceException("该银行卡号已被绑定了");
        }
        return savebackcard(0, cardTypeId, cardNumber, userName, 0, bussId);
    }

    /**
     * 检查银行卡是否绑定
     *
     * @param bussId     商家编号
     * @param cardTypeId 银行编号
     * @param cardNumber 银行卡号
     * @return
     */
    @Override
    public boolean checkBundling(Integer bussId, Integer cardTypeId, String cardNumber) throws ServiceException {
        UserBank userBank = userBankDao.getuserBankByCardNumber(cardNumber);
        if (userBank != null) {
            throw new ServiceException("该银行卡号已被绑定了");
        }
        return true;
    }

    /**
     * 添加配送员银行卡信息
     *
     * @param deliveryId 配送员编号
     * @param cardTypeId 银行类型
     * @param cardNumber 卡号
     * @param userName   用户名
     * @return
     */
    @Override
    public boolean addDeliveryBankCard(Integer deliveryId, Integer cardTypeId, String cardNumber, String userName) {
        return savebackcard(deliveryId, cardTypeId, cardNumber, userName, 0, 0);
    }

    /**
     * 功能描述:
     *
     * @param deliveryId 配送员编号
     * @param userName   持卡人姓名
     * @param cardNumber 银行卡编号
     * @param cardTypeId 银行卡类型编号
     * @param userId     用户编号
     * @param companyId  商家编号
     * @return:
     * @auther: wsl
     * @date: 2018/11/29 16:43
     */
    private boolean savebackcard(Integer deliveryId, Integer cardTypeId, String cardNumber, String userName, int userId, int companyId) {
        UserBank userBank = new UserBank();
        userBank.setBussineId(companyId);
        userBank.setUserId(userId);
        userBank.setCardNumber(cardNumber);
        userBank.setCardTypeId(cardTypeId);
        userBank.setCreated(new Date());
        userBank.setDeliveryId(deliveryId);
        userBank.setState(1);
        userBank.setUserName(userName);
        return userBankDao.save(userBank) > 0;

    }

    @Override
    public Map<String, Object> getDeliveryBankById(Integer bankcardId) {
        return userBankDao.getDeliveryBankById(bankcardId);
    }

    @Override
    public boolean checkBundlingByDisId(Integer disId, Integer bankId, String cardNumber) throws ServiceException {
        UserBank userBank = userBankDao.getuserBankByCardNumber(cardNumber);
        if (userBank != null) {
            if (userBank.getDeliveryId() != 0) {
                throw new ServiceException("该银行卡号已被绑定了");
            }
        }
        return true;
    }

    @Override
    public boolean addUserBankCard(Integer userId, Integer cardTypeId, String cardNumber, String userName) {
        return savebackcard(0, cardTypeId, cardNumber, userName, userId, 0);
    }

    @Override
    public boolean unBundlingByUserId(Integer userId, Integer bankId) throws ServiceException {
        UserBank userBank = userBankDao.getEntityById(bankId);
        if (userBank == null) {
            throw new ServiceException("银行卡不存在");
        }
        if (!Objects.equals(userId, userBank.getUserId())) {
            throw new ServiceException("您要解绑的银行卡不是您自己的银行卡");
        }
        return userBankDao.unBundling(bankId) > 0;
    }

    @Override
    public List<Map<String, Object>> findUserBankCard(Integer userId) {
        return userBankDao.findUserBankCard(userId);
    }

    @Override
    public Map<String, Object> findUserDefaultBankCard(Integer userId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("cashTime", paramsService.getParamsValue(29, 27).get("value"));
        List<Map<String, Object>> cashList = cashDao.findCashHistoryByUserId(userId);
        if (cashList != null && cashList.size() > 0) {
            Map<String, Object> cashMap = cashList.get(0);
            Integer bankcardId = ConvertTypeUtils.obj2Integer(cashMap.get("bankcardId"));
            Map<String, Object> userBank = userBankDao.getDeliveryBankById(bankcardId);
            if (userBank != null) {
                map.put("bankCard", userBank);
            } else {
                map.put("bankCard", new HashMap<>());
            }
        } else {
            //没有，默认排序第一张
            List<Map<String, Object>> mapList = userBankDao.findUserBankCard(userId);
            if (mapList != null && mapList.size() > 0) {
                Map<String, Object> userBank = mapList.get(0);
                map.put("bankCard", userBank);
            } else {
                map.put("bankCard", new HashMap<String, Object>());
            }
        }
        return map;
    }

    @Override
    public boolean cash(Integer userId, Integer bankCardId, Double money, Integer type) throws ServiceException {
        //银行卡信息不存在修改，问题是传值错误
        UserBank userBank = userBankDao.getEntityById(bankCardId);
        User user = userService.getUserBeanById(userId);
        Integer isCash = cashDao.getCountByUserId(userId);
        if (isCash > 0) {
            throw new ServiceException("已经发起过提现了，请耐心等待处理！");
        }
        Cash cash = new Cash();
        cash.setBankCard(userBank.getCardNumber());
        cash.setBankcardId(bankCardId);
        BankcardType bankcardType = bankcardTypeDao.getEntityById(userBank.getCardTypeId());
        if (bankcardType == null) {
            throw new ServiceException("银行不存在");
        }
        cash.setBankName(bankcardType.getCardName());
        cash.setCompanyId(0);
        cash.setDeliveryId(0);
        cash.setUserId(userId);
        cash.setType(type);
        cash.setCreated(new Date());
        cash.setMobile(user.getAccount());
        cash.setMoney(BigDecimal.valueOf(money));
        cash.setName(userBank.getUserName());
        cash.setState(1);

        return cashDao.save(cash) > 0;
    }
}
